local extension = Package:new("testing")
extension.extensionName = "skinchange"
Fk:loadTranslationTable{
  ["testing"] = "测试",
}
local U = require "packages/utility/utility"
local SL = require "packages/skinchange/skinslist"
local skin_tester = General(extension, "skin_tester", "qun", 3)
local testSkins = fk.CreateActiveSkill{
    name = "testSkins",
    prompt = "#testSkins-prompt",
    interaction = function(self)
        local chs = {"wei", "shu", "wu", "qun", "jin"}
        return UI.ComboBox {
            choices = chs
        }
      end,
    can_use = function(self, player)
        return true
    end,
    target_filter = Util.FalseFunc,
    card_filter = Util.FalseFunc,
    on_use = function(self, room, effect)
        local player = room:getPlayerById(effect.from)
        local kingdom = self.interaction.data
        local generals = {}
        local skins = {}
        
        for k, gt in pairs(SL.skinTable[kingdom]) do
            local g = SL.getGeneralsByName(nil, kingdom, nil, k, function(g2)
                return not table.contains(generals, g2.name)
            end)[1]
            if g then
                table.insert(generals, g.name)
            end
        end
        if #generals > 0 then
            local result = player.room:askForCustomDialog(player, "#huanfu",
            "packages/utility/qml/ChooseGeneralsAndChoiceBox.qml", {
                generals,
                {"OK"},
                "#huanfu-pick",
                {"Cancel"},
                1,
                1,
                {current_general},
            })
            local names = {}
            if result == "" then
                return
            else
                names = json.decode(result).cards
            end
            if #names > 0 then
                skins = SL.getSkins(names[1], kingdom)
            end
        end
        if #skins > 0 then
            local result = player.room:askForCustomDialog(player, "#huanfu",
            "packages/utility/qml/ChooseGeneralsAndChoiceBox.qml", {
                skins,
                {"OK"},
                "#huanfu-pick",
                {"Cancel"},
                1,
                1,
                {current_general},
            })
            local names = {}
            if result == "" then
                return
            else
                names = json.decode(result).cards
            end
            if #names > 0 then
                player.general = names[1]
                room:broadcastProperty(player, "general")
            end
        end
    end,
}
skin_tester:addSkill(testSkins)
skin_tester:addSkill("test_zhenggong")

local huanfu = fk.CreateActiveSkill{
    name = "huanfu&",
    mute = true,
    prompt = "#huanfu-prompt",
    can_use = function(self, player)
        if player:getMark("skins") ~= 0 and player:usedSkillTimes(self.name, Player.HistoryRound) == 0 then
            return #player:getMark("skins") > 1
        end
    end,
    target_filter = Util.FalseFunc,
    card_filter = Util.FalseFunc,
    on_use = function(self, room, effect)
        local player = room:getPlayerById(effect.from)
        local skins = player:getMark("skins")
        local deputy_skins = player:getMark("deputy_skins")
        local result = player.room:askForCustomDialog(player, "#huanfu",
        "packages/utility/qml/ChooseGeneralsAndChoiceBox.qml", {
            skins,
            {"OK"},
            "#huanfu-pick",
            {"Cancel"},
            1,
            1,
            {current_general},
        })
        local names = {}
        if result == "" then
            return
        else
            names = json.decode(result).cards
        end
        if #names > 0 then
            player.general = names[1]
            room:broadcastProperty(player, "general")
        end
    end,
}
local searchThenSetSkins = function(room, player)
    if player:getMark("skins") == 0 then
        local general = Fk.generals[player.general]
        local kingdom = general.kingdom
        if player:getMark("__hidden_general") == 0 then
            local skins = SL.getSkins(player.general, kingdom)
            local deputy_skins = {}
            if #(player.deputyGeneral or {}) > 0 then
                deputy_skins = SL.getSkins(player.deputyGeneral, kingdom)
            end
            if #skins > 0 then
                room:setPlayerMark(player, "skins", skins)
            end
            if #deputy_skins > 0 then
                room:setPlayerMark(player, "deputy_skins", deputy_skins)
            end
        else
            local skins = SL.getSkins(player:getMark("__hidden_general"), kingdom)
            local deputy_skins = SL.getSkins("__hidden_deputy", kingdom)
            if #skins > 0 then
                room:setPlayerMark(player, "skins", skins)
            end
            if #deputy_skins > 0 then
                room:setPlayerMark(player, "deputy_skins", deputy_skins)
            end
        end
    end
end
local huanfu_trigger = fk.CreateTriggerSkill{
    name = "#huanfu_trigger",
    global = true,
    priority = 0.0001,
    refresh_events = {fk.GamePrepared, fk.EventPhaseStart},
    can_refresh = function(self, event, target, player, data)
        if event == fk.GamePrepared then
            return true
        elseif event == fk.EventPhaseStart and player.phase == Player.Play then
            if player == target then
                return true
            end 
        end
    end,
    on_refresh = function(self, event, target, player, data)
        local room = player.room
        if event == fk.GamePrepared then
            --searchThenSetSkins(room, player) 开启此项后十常侍做副将会出bug
            for _, p in ipairs(room.alive_players) do
            room:handleAddLoseSkills(p, "huanfu&", nil, false, true)
            end
        else
            searchThenSetSkins(room, player)
        end
    end,
}
Fk:addSkill(huanfu_trigger)
Fk:addSkill(huanfu)
Fk:loadTranslationTable{
    ["skin_tester"] = "皮肤测试员",
    ["#huanfu-prompt"] = "你可以更换武将皮肤",
    ["huanfu&"] = "换肤",
    [":huanfu&"] = "你可以于出牌阶段更换皮肤。",
    ["#huanfu"] = "换肤",
    ["#huanfu-pick"] = "选择更换皮肤",
    ["testSkins"] = "换测",
    ["#testSkins-prompt"] = "更换任意皮肤",
}
return extension